//
//  ZGViewController.m
//  ZGAudioDetective
//
//  Created by kidzss on 09/14/2021.
//  Copyright (c) 2021 kidzss. All rights reserved.
//

#import "ZGViewController.h"
#include <ZGAudioDetective/LBAudioDetective.h>
#import <ZGAudioDetective/NSBundle+BundleExtension.h>
#import <ZGAudioDetective/ZGAudioFingerPrintView.h>

#define kScreenWidth   [UIScreen mainScreen].bounds.size.width

#define kScreenHeight  [UIScreen mainScreen].bounds.size.height

@interface ZGViewController ()

@property(nonatomic,strong) UILabel *cLab;
@property(nonatomic,strong) UILabel *cLab1;
@end

@implementation ZGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    LBAudioDetectiveRef detective = LBAudioDetectiveNew();
    Float32 match = 0.0f;
    
    NSURL *firstURL = [self ex_AudioPath:@"BlueTit" fileExtension:@"caf"];
    
    NSURL *secondURL = [self ex_AudioPath:@"BlackBird" fileExtension:@"caf"];

    self.cLab1.text = [NSString stringWithFormat:@"声波纹对比:"];
    [self.view addSubview:self.cLab1];
    
    ZGAudioFingerPrintView *audio1 = [[ZGAudioFingerPrintView alloc] initWithFrame:CGRectMake(10, 150, kScreenWidth-20, 100)];
    audio1.url = firstURL;
    [self.view addSubview:audio1];
    
    ZGAudioFingerPrintView *audio2 = [[ZGAudioFingerPrintView alloc]initWithFrame:CGRectMake(10, 360, kScreenWidth-20, 100)];
    audio2.url = secondURL;
    [self.view addSubview:audio2];
    
    //计算两者差异
    
    LBAudioDetectiveCompareAudioURLs(detective, firstURL, secondURL, 0, &match);
    NSLog(@"The files are equal to a percentage of %f", match);
    LBAudioDetectiveDispose(detective);
    
    self.cLab.text = [NSString stringWithFormat:@"声纹相似度:%f",match];
    [self.view addSubview:self.cLab];
}

- (UILabel *)cLab {
    if (!_cLab) {
        _cLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 520, 200, 30)];
        _cLab.textColor = UIColor.blueColor;
        _cLab.font = [UIFont systemFontOfSize:16];
    }
    return _cLab;
}

- (UILabel *)cLab1 {
    if (!_cLab1) {
        _cLab1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 70, 200, 30)];
        _cLab1.textColor = UIColor.blueColor;
        _cLab1.font = [UIFont systemFontOfSize:16];
    }
    return _cLab1;
}

/// 播放通知铃声
- (NSURL *)ex_AudioPath:(NSString *)filename fileExtension:(NSString *)fileExtension {
    // 转换 URL
    NSString *path = [[NSBundle ex_mainBundle] pathForResource:filename ofType:fileExtension];
    if (path && [path isKindOfClass:[NSString class]]) {
        NSURL *url = [NSURL fileURLWithPath:path];
        return url;
    }
    return nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
